package cn.rkylin.oms.system.user.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rkylin.core.controller.ApolloController;
import cn.rkylin.oms.common.context.CurrentUser;
import cn.rkylin.oms.common.context.WebContextFactory;

/**
 * 用户控制器
 * 
 * @author wangxiaoyi
 * @version 1.0
 * @created 2017年2月15日
 */
@Controller
@RequestMapping("/user")
public class UserController extends ApolloController {
	private static final Log logger = LogFactory.getLog(UserController.class);
	// 常量定义
	private static final String GOTO_INDEX = "redirect:/index.html"; //s 跳转到首页

	/**
	 * 构造函数
	 */
	public UserController() {

	}


	/**
	 * 跳转到店铺列表
	 */
	@RequestMapping(value = "/gotoIndex")
	public String gotoShopList() {
		return GOTO_INDEX;
	}

	/**
	 * 登录
	 * 
	 * @param account 登录帐户
	 * @param password 登录密码
	 * @param captcha 验证码
	 * @return 成功转到首页 失败给出错误提示
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public Map<String, Object> login(String account, String password, String captcha) throws Exception {
		Map<String, Object> returnMap = new HashMap<String, Object>();
		try {
			// TODO 这里应该访问oms系统进行登录
			CurrentUser currUser = new CurrentUser();
			currUser.setId("admin");
			currUser.setFullName("超级管理员");
			//currUser.setOrgId("rootOrg");
			//currUser.setOrgName("瑞金麟集团");
			WebContextFactory.getWebContext().setCurrentUser(currUser);
			returnMap.put(JSON_RESULT, SUCCESS);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			returnMap.put(JSON_RESULT, FAILED);
			returnMap.put(JSON_MSG, ex.getMessage());
		}
		return returnMap;
	}
}